package com.wy.kdemo

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.wy.kdemo.adapter.ViewPageAdapter
import com.wy.kdemo.fragment.CustomerFragment
import com.wy.kdemo.fragment.HomeFragment
import com.wy.kdemo.fragment.MeFragment
import com.wy.kdemo.fragment.YXFragment
import com.wy.kdemo.fragment.ZYFragment
import com.wy.kdemo.view.NoAnimationViewPager

class MainActivity : AppCompatActivity() {

    private lateinit var viewPager: NoAnimationViewPager
    private lateinit var layoutTab: TabLayout

    private val ivTabsNormal = arrayOf(
        R.drawable.icon_home,
        R.drawable.icon_customer,
        R.drawable.icon_yx,
        R.drawable.icon_zy,
        R.drawable.icon_me
    )
    private val ivTabsPressed = arrayOf(
        R.drawable.icon_home_press,
        R.drawable.icon_customer_press,
        R.drawable.icon_yx_press,
        R.drawable.icon_zy_press,
        R.drawable.icon_me_press
    )
    private var tvTabs = arrayOf(
        "首页",
        "客户",
        "营销",
        "展业",
        "我的"
    )


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewPager = findViewById(R.id.view_pager)
        layoutTab = findViewById(R.id.layout_tab)
        initTab()
    }

    @SuppressLint("MissingInflatedId")
    private fun initTab() {
        val fragments: List<Fragment> = listOf(
            HomeFragment(),
            CustomerFragment(),
            YXFragment(),
            ZYFragment(),
            MeFragment()
        )

        viewPager.adapter = ViewPageAdapter(supportFragmentManager, fragments)
        layoutTab.setupWithViewPager(viewPager)
        layoutTab.tabMode = TabLayout.MODE_FIXED

        for (i in fragments.indices) {
            val tabView: View = LayoutInflater.from(this).inflate(R.layout.item_tab, null)
            val tv: TextView = tabView.findViewById(R.id.tv_tab)
            val iv: ImageView = tabView.findViewById(R.id.iv_tab)
            layoutTab.getTabAt(i)?.setCustomView(tabView)

            if (i == 0) {
                iv.setImageResource(ivTabsPressed[0])
                tv.setTextColor(getColor(R.color.red))
            } else {
                tv.text = tvTabs[i]
                iv.setImageResource(ivTabsNormal[i])
            }
        }
        layoutTab.setSelectedTabIndicator(null)
        layoutTab.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                if (tab != null) {
                    viewPager.setCurrentItem(tab.position, false)
                }
                val tabView = tab?.customView
                val tv: TextView? = tabView?.findViewById(R.id.tv_tab)
                val iv: ImageView? = tabView?.findViewById(R.id.iv_tab)
                iv?.setImageResource(ivTabsPressed[tab.position])
                tv?.setTextColor(getColor(R.color.red))
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                val tabView = tab?.customView
                val tv: TextView? = tabView?.findViewById(R.id.tv_tab)
                val iv: ImageView? = tabView?.findViewById(R.id.iv_tab)
                iv?.setImageResource(ivTabsNormal[tab.position])
                tv?.setTextColor(getColor(R.color.font_black))
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })

    }

}
